import _config from '../../utils/config';
import _fetch from '../../utils/httpService';

Page({
  data: {
    customerList: [],
    inputText: '',
    focusSearch: false,
    isSearchClearShow: false,
    current: {},
    query: null
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    console.log(options)
    this.setData({
      query: options
    })
    this.loadList()
  },
  onReady: function () {},
  onShow: function () {
    // 生命周期函数--监听页面显示
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏
  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
  },
  onShareAppMessage: function () {
    // 用户点击右上角分享
    return {
      title: '助力企业工单管理智能升级', // 分享标题
      desc: '', // 分享描述
      path: '/pages/login/index', // 分享路径
      imageUrl: '../../img/Mask.png' // 分享图片
    }
  },
  focusSearchInput() {
    this.setData({
      focusSearch: true
    })
  },
  showClear(e) {
    if (e.detail.value) {
      this.setData({
        inputText: e.detail.value,
        isSearchClearShow: true,
        focusSearch: true
      })
    } else {
      this.setData({
        inputText: e.detail.value,
        isSearchClearShow: false,
        focusSearch: true
      })
    }
  },
  toSearch(e) {
    if (e.detail.value) {
      this.setData({
        inputText: e.detail.value
      })
      this.loadList()
    } else {
      this.setData({
        inputText: e.detail.value
      })
    }
  },
  tapSearch() {
    this.loadList()
  },
  tapSearchClear() {
    this.setData({
      isSearchClearShow: false,
      inputText: '',
      scrollTop: 0,
      focusSearch: this.data.focusSearch
    })
    this.loadList()
  },
  loadList() {
    let params = {
        name: this.data.inputText
      },
      vm = this
    _fetch.get(_config.url.get_customer, params).then(res => {
      vm.setData({
        customerList: res || []
      })
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none',
      });
    })
  },
  toChoose(e) {
    if (e.currentTarget.dataset.citem) {
      this.setData({
        current: e.currentTarget.dataset.citem
      })
    }
  },
  confirmPoint() {
    let index = this.data.query.index,
      form = JSON.parse(this.data.query.form),
      name = this.data.query.name,
      tid = this.data.query.id,
      current = this.data.current
    form[index].componentValue = Object.assign(form[index].componentValue, {
      customerId: current.customerId,
      customerName: current.customerName
    })
    let params = form.map(i => {
      if (i.title == '联系人') {
        i.componentValue = current.linkman || ''
      }
      if (i.title == '联系电话') {
        i.componentValue = current.phone || ''
      }
      if (i.title == '详细地址') {
        i.componentValue = current.detailAddress || ''
      }
      if (i.title == '服务地址') {
        if (current.type == 1) {
          i.region = [current.provinceName, current.cityName, current.areaName]
          i.componentValue = {
            addressType: current.type,
            addressValue: {
              provinceId: current.provinceId || '',
              provinceName: current.provinceName || '',
              cityId: current.cityId || '',
              cityName: current.cityName || '',
              areaId: current.areaId || '',
              areaName: current.areaName || ''
            }
          }
        } else {
          i.region = []
          i.componentValue = {
            addressType: current.type,
            addressValue: {
              adCode: current.adCode || '',
              latitude: current.latitude || '',
              longitude: current.longitude || '',
              location: current.location || ''
            }
          }
        }
      }
      return i
    })
    console.log(params)
    setTimeout(() => {
      if (this.data.query.enter == 'order') {
        wx.navigateTo({
          url: `../orderAdd/index?id=${tid}&name=${name}&form=${JSON.stringify(params)}&phone=${current.phone}&cusname=${current.customerName}&cusindex=${this.data.query.cusindex}`
        })
      } else {
        wx.navigateTo({
          url: `../progress/index?id=${tid}&formid=${this.data.query.formid}&form=${JSON.stringify(params)}&list=${this.data.query.list}&curidx=${this.data.query.curidx}&phone=${current.phone}&cusname=${current.customerName}&cusindex=${this.data.query.cusindex}`
        })
      }
    }, 0)
  }
})